function PLUGIN:Initialize() 

end

-- NoCollide-All (Selection will only collide with the world)
function PLUGIN:noCollideAll(ply, args)
	if CLIENT then return end
	
	local ent = ply:GetEyeTraceNoCursor().Entity
	local collision = tobool(args[1]) and COLLISION_GROUP_WORLD or COLLISION_GROUP_NONE

	if ecs.Selections[ply] then
		for k, ent in pairs(ecs.Selections[ply]) do
			ent:SetCollisionGroup(collision)
			ent.CollisionGroup = collision
		end
	end	
	if ecs.CanManipulate(ply , ent) then
		ent:SetCollisionGroup(collision)
		ent.CollisionGroup = collision
	end
end
PLUGIN:AddServerCommand( "nocollideall" , PLUGIN.noCollideAll )

-- NoCollide (Selection is no-collided to the entity you are looking at)
function PLUGIN:noCollideTo(ply, args)
	if CLIENT then return end

	local trace = ply:GetEyeTraceNoCursor().Entity

	if ecs.CanManipulate(ply , trace) then
		if ecs.Selections[ply] then
			for k, ent in pairs(ecs.Selections[ply]) do
				local constraint = constraint.NoCollide(ent, trace, 0, 0)				
			end
		end	
	end
end
PLUGIN:AddServerCommand( "nocollide" , PLUGIN.noCollideTo )

-- Weld (Selection is welded to the entity you are looking at)
function PLUGIN:weldTo(ply, args)
	if CLIENT then return end
	
	local trace = ply:GetEyeTraceNoCursor().Entity
	local forcelimit = tonumber(args[1])
	local untilbreak = tobool(args[2])	

	if ecs.CanManipulate(ply , trace) then -- Need another CanManipulate function that includes the world
		if ecs.Selections[ply] then
			for k, ent in pairs(ecs.Selections[ply]) do
				local constraint = constraint.Weld(ent, trace, 0, 0, forcelimit, untilbreak)				
			end
		end	
	end
end
PLUGIN:AddServerCommand( "weld" , PLUGIN.weldTo )

-- Parenting (Selection is parented to the entity you are looking at)
function PLUGIN:parentTo(ply, args)
	if CLIENT then return end
	
	local trace = ply:GetEyeTraceNoCursor().Entity
	local nocollide = tobool(args[1])	

	if ecs.CanManipulate(ply , trace) then
		if ecs.Selections[ply] then
			for k, ent in pairs(ecs.Selections[ply]) do
				if ent != trace then	-- Make sure not to parent an entity to itself
					local physobj = ent:GetPhysicsObject()

					if physobj:IsValid() then
						if nocollide then constraint.NoCollide(ent, trace, 0, 0) end
						physobj:EnableCollisions( false )
						physobj:EnableMotion( false )
						physobj:Sleep()
						ent:SetParent( trace )		-- fuck with Entity.PhysgunDisabled
					end
				end
			end
		end	
	end
end
PLUGIN:AddServerCommand( "parent" , PLUGIN.parentTo )

-- Unparenting (Selection is unparented)
function PLUGIN:unParent(ply, args)
	if CLIENT then return end

	if ecs.Selections[ply] then
		for k, ent in pairs(ecs.Selections[ply]) do
			local position = ent:GetPos()
			local physobj  = ent:GetPhysicsObject()

			if physobj:IsValid() then
				physobj:EnableCollisions( true )
				physobj:EnableMotion( false )
				physobj:Sleep()
				ent:SetParent()
				ent:SetPos( position )
			end
		end
	end
end
PLUGIN:AddServerCommand( "unparent" , PLUGIN.unParent )

-- Remove Constraints (Removes all constraints, or if given, the specified type)
function PLUGIN:removeConstraints(ply, args)
	if CLIENT then return end
	
	local ent = ply:GetEyeTraceNoCursor().Entity
	local remove
	local ctype = tostring(args[1])

	if ecs.Selections[ply] then
		for k, ent in pairs(ecs.Selections[ply]) do
			if (ctype == "all") then
				remove = constraint.RemoveAll(ent)
			else
				remove = constraint.RemoveConstraints(ent, ctype)
			end
		end
		return remove
	end
	if ecs.CanManipulate(ply , ent) then
			if (ctype == "all") then
				remove = constraint.RemoveAll(ent)
			else
				remove = constraint.RemoveConstraints(ent, ctype)
			end
		return remove
	end
end
PLUGIN:AddServerCommand( "rc" , PLUGIN.removeConstraints )
--PLUGIN:AddServerCommand( "rc" , PLUGIN.removeConstraints , { "All" , "Weld", "Rope", "AdvBallsocket", "Axis", "Ballsocket", "Elastic", "Hydraulic", "KeepUpright", "Muscle", "Pulley", "Winch", "Slider"})